/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package memory;

import java.util.ArrayList;

/**
 *
 * @author Erdiansyah
 */
public class Container {
    public ArrayList<Room> roomlist;
    public ArrayList<User> onlineuserlist;
    public ArrayList<User> userfriendlist;
    public ArrayList<Chatroom> chatroomlist;
    public ArrayList<UserBanned> userbannedlist;

    public Container() {
        roomlist = new ArrayList<Room>();
        userfriendlist = new ArrayList<User>();
        chatroomlist = new ArrayList<Chatroom>();
        userbannedlist = new ArrayList<UserBanned>();
    }

    public ArrayList<User> getOnlineuserlist() {
        return onlineuserlist;
    }

    public void setOnlineuserlist(ArrayList<User> onlineuserlist) {
        this.onlineuserlist = onlineuserlist;
    }

    public boolean isExistOnlineUser(User user){
        return onlineuserlist.contains(user);
    }

    public void addToOnlineUser(User user){
        onlineuserlist.add(user);
    }
    public void removeUserFromOnlineUser(User user){
        onlineuserlist.remove(user);
    }

    public int getIndexOfOnlineUser(String user){
        for(int i=0;i<onlineuserlist.size();++i){
            if(onlineuserlist.get(i).username.equalsIgnoreCase(user))
                return i;
        }
        return  0;
    }


    public User getOnlineUser(String user){
        return onlineuserlist.get(getIndexOfFriend(user));
    }

    public ArrayList<User> getUserfriendlist() {
        return userfriendlist;
    }

    public void setUserfriendlist(ArrayList<User> userfriendlist) {
        this.userfriendlist = userfriendlist;
    }

    public boolean isExistFriend(User user){
        return  userfriendlist.contains(user);
    }

    public void addToUserFriendList(User user){
        userfriendlist.add(user);
    }

    public void removeFriendList(User user){
        userfriendlist.remove(user);
    }
    
    public int getIndexOfFriend(String user){
        for(int i=0;i<userfriendlist.size();++i){
            if(userfriendlist.get(i).username.equalsIgnoreCase(user))
                return i;
        }
        return 0;
    }

    public User getFriend(String user){
        return userfriendlist.get(getIndexOfFriend(user));
    }


    public boolean isExistRoom(Room room){
        return roomlist.contains(room);
    }

    public ArrayList<Room> getRoomlist() {
        return roomlist;
    }

    public void setRoomlist(ArrayList<Room> roomlist) {
        this.roomlist = roomlist;
    }
    public void addRoomToList(Room room){
        roomlist.add(room);
    }

    public void removeRoomToList(Room room){
        roomlist.remove(room);
    }

    public int getIndexOfRoom(String roomname){
        for(int i=0;i< roomlist.size();++i){
            if(roomlist.get(i).roomname.equalsIgnoreCase(roomname))
                return i;
        }
        return 0;
    }

    public Room getRoom(String roomname){
        return roomlist.get(getIndexOfRoom(roomname));
    }

    public boolean isExistChatroom(Chatroom chatroom){
        return chatroomlist.contains(chatroom);
    }

    public ArrayList<Chatroom> getChatroomlist() {
        return chatroomlist;
    }

    public void setChatroomlist(ArrayList<Chatroom> chatroomlist) {
        this.chatroomlist = chatroomlist;
    }

    public void addChatroomToList(Chatroom chatroom){
        chatroomlist.add(chatroom);
    }

    public void removeChatroomFromList(Chatroom chatroom){
        chatroomlist.remove(chatroom);
    }

    public int getIndexOfChatroom(String user1,String user2){
        int temp = 0;
        for(int i=0; i<chatroomlist.size();++i){
            if((chatroomlist.get(i).getUsername().username.equalsIgnoreCase(user1) && chatroomlist.get(i).getTousername().username.equalsIgnoreCase(user2))||
                    (chatroomlist.get(i).getUsername().username.equalsIgnoreCase(user2) && chatroomlist.get(i).getTousername().username.equalsIgnoreCase(user1)))
            //if(chatroomlist.get(i).id.equalsIgnoreCase("input_"+user1+"_"+user2)){
            temp = i;
            //}
        }
        return temp;
    }

    public Chatroom getChatroom(String user1,String user2){
        return chatroomlist.get(getIndexOfChatroom(user1, user2));
    }

    public Chatroom getChatroom(String name){
        for(int i=0; i<chatroomlist.size();++i){
            if (chatroomlist.get(i).getId().equalsIgnoreCase(name)){
                return chatroomlist.get(i);
            }
        }
        return null;
    }

    public boolean isExistUserBanned(UserBanned user){
        return userbannedlist.contains(user);
    }

    public ArrayList<UserBanned> getUserbannedlist() {
        return userbannedlist;
    }

    public void setUserbannedlist(ArrayList<UserBanned> userbannedlist) {
        this.userbannedlist = userbannedlist;
    }

    public void addToUserbannedList(UserBanned user){
        userbannedlist.add(user);
    }

    public void removeUserbannedFromList(UserBanned user){
        userbannedlist.remove(user);
    }

    public int getIndexOfUserBannedList(String user){
        for(int i= 0; i< userbannedlist.size();++i){
            if(userbannedlist.get(i).user.username.equalsIgnoreCase(user)){
            return i;
            }
        }
        return 0;
    }

    public UserBanned getUserBanned(String user){
        return userbannedlist.get(getIndexOfUserBannedList(user));
    }
}
